package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.vo.Class;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IClassDAO;

public class ClassDAO extends BaseDAO implements IClassDAO {

	@Override
	public void addClass(Class cla) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(cla);
		tx.commit();
		session.close();

	}

	@Override
	public void deleteClass(Class cla) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Class as c where c.classId = "+cla.getClassId()+"";
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();

	}

	@Override
	public void modifyClass(Class cla) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Class set name=:name, description=:description where classId=:classId";
		Query query = session.createQuery(hql);
		query.setString("name", cla.getName());
		query.setString("description", cla.getDescription());
		query.setInteger("classId", cla.getClassId());
		query.executeUpdate();
		tx.commit();
		session.close();

	}

	@Override
	public List<Class> searchAllClass() {
		
		Session session = getSession();
		String hql = "from Class";
		Query query = session.createQuery(hql);
		List<Class> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

}
